package com.aegean.CommercialHotelBookSystem.room.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aegean.CommercialHotelBookSystem.factory.BusinessFactory;
import com.aegean.CommercialHotelBookSystem.factory.MyTools;
import com.aegean.CommercialHotelBookSystem.model.Hotel;
import com.aegean.CommercialHotelBookSystem.room.business.IRoomBusiness;

/**
 * Servlet implementation class AddRoom
 */
public class AddRoom extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddRoom() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String Hotel_Name = null;
		String Room_Name = null;
		float Room_TotalMoney;
		float Room_Discount;
		int Room_Count;
		String Room_Type = null;
		String Room_Date = null;
		int Hotel_Id = 1;
		if (request.getParameter("tag") != null
				&& request.getParameter("tag").equals("0")) {
			Hotel_Name = request.getParameter("Hotel_Name");
			IRoomBusiness business = BusinessFactory.createRoomBusiness();
			boolean result = business.searchReHotel(MyTools
					.toChinese(Hotel_Name));
			if (result == true) {
				out.println(true);
			} else {
				out.println(false);
			}
		} else {
			Hotel_Name = MyTools.toGB2312(request.getParameter("Hotel_Name"));
			Room_Name = MyTools.toGB2312(request.getParameter("Room_Name"));
			Room_TotalMoney = Float.parseFloat(request
					.getParameter("Room_TotalMoney"));
			Room_Discount = Float.parseFloat(request
					.getParameter("Room_Discount"));
			Room_Count = Integer.parseInt(request.getParameter("Room_Count"));
			Room_Type = MyTools.toGB2312(request.getParameter("Room_Type"));
			Room_Date = request.getParameter("Room_Date");
			HttpSession session = request.getSession();
			Hotel one = (Hotel) session.getAttribute("Hotel_Login");
			if (one == null) {
				Hotel_Id = 0;
			} else {
				Hotel_Id = one.getHotel_Id();
				Hotel_Name = null;
			}
			IRoomBusiness business = BusinessFactory.createRoomBusiness();
			boolean result = business.addRoom(MyTools.toChinese(Hotel_Name),
					Hotel_Id, Room_Name, Room_TotalMoney, Room_Discount,
					Room_Count, Room_Type, Room_Date);
			if (result == true) {
				request.getRequestDispatcher("Room_insert.jsp").forward(
						request, response);
			} else {
				request.getRequestDispatcher("Room_insert.jsp").forward(
						request, response);
			}
		}
	}
}
